package com.qk.ezbuy.controller;

import com.github.pagehelper.PageInfo;
import com.qk.ezbuy.pojo.News;
import com.qk.ezbuy.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/News")
public class NewsConterller {
    @Autowired
    private NewsService newsService;
    @RequestMapping("/NewsTo")
    public PageInfo<News> selectByPage(@RequestParam int pageNum, @RequestParam int pageSize){
        PageInfo<News> pageInfo=null;
        pageInfo=newsService.selectPage(pageNum,pageSize);
        return pageInfo;
    }

    @RequestMapping("/NewsById")
    public Map NewsById(int id){
       Map<String,Object> map = new HashMap<>();
       News news =  newsService.selectNews(id);
       map.put("news",news);
       return map;
    }

    @RequestMapping("/NewsUpdate")
    public Map NewsUpdate(News news){
        System.out.println("=================xiugai");
        Map<String,Object> map = new HashMap<>();
        int count = newsService.updateByPrimaryKeySelective(news);
        System.out.println(news.getTitle()+news.getContent());
        System.out.println(count);
        if(count>0){
            map.put("msg",1);
        }else{
            map.put("msg",2);
        }
        return map;
    }

    @RequestMapping("NewsDel")
    public Map NewsDel(int id){
        Map<String,Object> map = new HashMap<>();
        int count = newsService.delNews(id);
        if(count>0){
            map.put("msg",1);
        }else{
            map.put("msg",2);
        }
        return map;
    }

    @RequestMapping("NewsAdd")
    public Map NewsAdd(News news){
        System.out.println("sad");
        Map<String,Object> map = new HashMap<>();
        news.setCreatetime(new Date());
        int count = newsService.insertSelective(news);
        if(count>0){
            map.put("msg",1);
        }else{
            map.put("msg",2);
        }
        return map;
    }
}
